//
//  SettingsEntryView.m
//  WeightMaster
//
//  Created by sungeo on 15/8/22.
//  Copyright (c) 2015年 buddysoft. All rights reserved.
//

#import "SettingsEntryView.h"
#import "WMModels.h"
#import "BDFoundation.h"
#import <Masonry.h>
#import <EXTScope.h>

@interface SettingsEntryView (){
    __weak id _clickedListener;
    SEL _action;
}

@end

@implementation SettingsEntryView

- (instancetype)init{
    if (self = [super init]) {
        
        @weakify(self);
        _backgroundImage = [[UIImageView alloc] init];
        [_backgroundImage addAction:@selector(backgroundImageClicked:) target:self];
        [self addSubview:_backgroundImage];
        [_backgroundImage mas_makeConstraints:^(MASConstraintMaker * maker){
            @strongify(self);
            maker.edges.equalTo(self);
        }];
        
        _textLabel = [[UILabel alloc] init];
        _textLabel.backgroundColor = [UIColor clearColor];
        _textLabel.font = [UIFont systemFontOfSize:20.0];
        _textLabel.textAlignment = NSTextAlignmentCenter;
        _textLabel.textColor = [UIColor whiteColor];
        _textLabel.shadowColor = RGB(82, 82, 97);
        _textLabel.shadowOffset = CGSizeMake(0.5, 0.5);
        [self addSubview:_textLabel];
        [_textLabel mas_makeConstraints:^(MASConstraintMaker * maker){
            @strongify(self);
            maker.leading.equalTo(self.mas_left);
            maker.trailing.equalTo(self.mas_right);
            maker.top.equalTo(self.mas_top).offset(18);
            maker.bottom.equalTo(self.mas_bottom);
        }];
        
        [self resetContent];
    }
    
    return self;
}

- (void)resetContent{
    WMAppSetting * setting = [WMAppSetting sharedInstance];
    if ([setting isSetGoalWeight]) {
        _backgroundImage.image = [UIImage imageNamed:@"index_goal"];
        _textLabel.text = [WMHelper stringValueForWeight:setting.goalWeight withUnit:setting.weightUnit];
    }else{
        _backgroundImage.image = [UIImage imageNamed:@"index_add_goal"];
    }
}

- (void)backgroundImageClicked:(id)sender{
    NSLog(@"设置背景被点击");
    if (_clickedListener && [_clickedListener respondsToSelector:_action]) {
        [_clickedListener performSelector:_action withObject:self];
    }
}

- (void)addClickedTarget:(id)target selector:(SEL)action{
    _clickedListener = target;
    _action = action;
}

+ (BOOL)requiresConstraintBasedLayout{
    return YES;
}

@end
